﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using ConfExpert.Utility.Peripheral;
using DevExpress.XtraEditors;
using DevTeam;

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class ParcaGirisi : XForm
    {
        public ParcaGirisi()
        {
            InitializeComponent();
        }

        private void ParcaGirisi_Load(object sender, EventArgs e)
        {
            
            btnKaydet.Enabled = false;
            gridView1.Init("GetKucukParcaGirisi");
            gridView1.AddSummary("Qty");
            gridView1.MakeReadOnly();
            gridView1.BestFitColumns();

            calcEdit1.EditValueChanged += calcEdit1_EditValueChanged;
            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnKaydetBig.ItemClick += btnKaydetBig_ItemClick;

            if (gridView1.Dataset().Tables[1].Rows.Count > 0)
                txtInfo.Caption = gridView1.Dataset().Tables[1].Rows[0]["GelisTarihi"].ToString() + " * GPId: " + gridView1.Dataset().Tables[1].Rows[0]["GelisPartisiId"].ToString();
        }

        void btnKaydetBig_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Kyt(true);
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Kyt(); 
        }


        void Kyt(bool big = false)
        {
            gridView1.EndEditing();
            if ((decimal)calcEdit1.EditValue > 0)
                try
                {
                    if (big)
                        big = (XtraMessageBox.Show("Bu seçenek, rapor boyu 10 cm.'den büyük kumaşlar içindir. Rapor boyu 10 cm.'den büyük mü?", "Dikkat", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes);

                    this.Cursor = Cursors.WaitCursor;


                    //Scanner scanner = new Scanner();
                    Image scanned = Scanner.Scan(big);    //kumaş için sabitlendi

                    Scanner.PhotosTable photosTable = new Scanner.PhotosTable();
                    photosTable.AddRow(0, scanned, big); //ProductId SP içinde setleniyor

                    //ds.xPhoto5.Clear();
                    //ds.xPhoto5.AddxPhoto5Row(1, ConvertImageToByteArray(i2, System.Drawing.Imaging.ImageFormat.Png), string.Empty);
                    ExecuteNonQuery("UpdKucukParcaGirisi", (decimal)calcEdit1.EditValue, photosTable.Table, Environment.MachineName, (big ? "big" : string.Empty));
                    //AppSession.MainForm.RefreshData();

                    gridView1.RefreshDataSource("GetKucukParcaGirisi");
                    gridView1.BestFitColumns();
                    gridView1.MoveLast();
                    calcEdit1.EditValue = null;
                    GC.Collect();

                    AppSession.MainForm.NotifyMain("Kaydedildi");
                    txtInfo.Caption = gridView1.Dataset().Tables[1].Rows[0]["GelisTarihi"].ToString() + ". GPId: " + gridView1.Dataset().Tables[1].Rows[0]["GelisPartisiId"].ToString();
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }

        }

        void calcEdit1_EditValueChanged(object sender, EventArgs e)
        {
            btnKaydet.Enabled = decimalParse(calcEdit1.EditValue) > 0;
        }


        //void ExecQueryTV(string SP, params object[] p)
        //{
        //    using (SqlConnection sqlConn = new SqlConnection(Session.DataManager.ConnectionString))
        //    {
        //        sqlConn.Open();
        //        SqlCommand sqlCmd = sqlConn.CreateCommand();
        //        sqlCmd.CommandText = SP;
        //        sqlCmd.CommandType = CommandType.StoredProcedure;
        //        SqlParameter[] parameters = new SqlParameter[3];
        //        parameters[0] = new SqlParameter("@Qty", SqlDbType.Decimal);
        //        parameters[0].Value = p[0];
        //        parameters[1] = new SqlParameter("@tbl", SqlDbType.Structured);
        //        parameters[1].Value = p[1];
        //        parameters[2] = new SqlParameter("@DeviceID", SqlDbType.VarChar);
        //        parameters[2].Value = p[2];
        //        sqlCmd.Parameters.AddRange(parameters);
        //        sqlCmd.ExecuteNonQuery();
        //    }
        //}
    }
}
